package com.yshkj.payment.service.impl;

import com.yshkj.payment.dto.InitDto;
import com.yshkj.payment.entity.Privilege;
import com.yshkj.payment.entity.User;
import com.yshkj.payment.entity.UserAccount;
import com.yshkj.payment.entity.UserPrivilege;
import com.yshkj.payment.repository.PrivilegeRepository;
import com.yshkj.payment.repository.UserAccountRepository;
import com.yshkj.payment.repository.UserPrivilegeRepository;
import com.yshkj.payment.repository.UserRepository;
import com.yshkj.payment.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * Created by 唐旭 on 2016/10/20.
 */
@Service
public class SystemServiceImpl implements SystemService {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserAccountRepository accountRepository;
    @Autowired
    private PrivilegeRepository privilegeRepository;
    @Autowired
    private UserPrivilegeRepository userPrivilegeRepository;


    @Override
    public InitDto initData() {
        InitDto initDto = new InitDto();
        List<UserAccount> userAccounts = accountRepository.findAll();
        if (!userAccounts.isEmpty()) {
            List<UserAccount> all = accountRepository.findAll();
            initDto.setUserAccount(all.get(0));
            return initDto;
        }

        User user = new User();
        user.setName("杨毅诊所");
        userRepository.save(user);
        initDto.setUser(user);
        UserAccount userAccount = new UserAccount(user);
        userAccount.setUsername("test");
        userAccount.setPassword("test");
        accountRepository.save(userAccount);
        initDto.setUserAccount(userAccount);

        Privilege medicinal = new Privilege();
        medicinal.setName("MEDICINAL_MANAGE");
        medicinal.setIcon("fa fa-institution");
        medicinal.setLabel("药品管理");
        medicinal.setOrdered(1);
        privilegeRepository.save(medicinal);
        Privilege medicinalInfo = new Privilege();
        medicinalInfo.setName("MEDICINAL_INFO_MANAGE");
        medicinalInfo.setIcon("fa fa-building");
        medicinalInfo.setUrl("index.medicinal");
        medicinalInfo.setLabel("药品信息管理");
        medicinalInfo.setOrdered(2);
        medicinalInfo.setParent(medicinal);
        privilegeRepository.save(medicinalInfo);
        Privilege producer = new Privilege();
        producer.setName("PRODUCER_MANAGE");
        producer.setIcon("fa fa-truck");
        producer.setLabel("厂家管理");
        producer.setOrdered(2);
        privilegeRepository.save(producer);
        Privilege storage = new Privilege();
        storage.setName("STORAGE_MANAGE");
        storage.setIcon("fa fa-book");
        storage.setLabel("库存管理");
        storage.setOrdered(3);
        privilegeRepository.save(storage);
        Privilege sale = new Privilege();
        sale.setName("SALE_MANAGE");
        sale.setIcon("fa fa-bar-chart-o");
        sale.setLabel("销售管理");
        sale.setOrdered(4);
        privilegeRepository.save(sale);

        Set<UserPrivilege> privilegeHashSet = userAccount.getPrivilegeHashSet();
        List<Privilege> all = privilegeRepository.findAll();
//        initDto.getPrivilegeList().addAll(all);
        for (Privilege privilege : all) {
            UserPrivilege userPrivilege = new UserPrivilege(userAccount);
            userPrivilege.setPrivilege(privilege);
            userPrivilegeRepository.save(userPrivilege);
            privilegeHashSet.add(userPrivilege);
        }
        initDto.setSuccess(true);
//        initDto.getUserPrivileges().addAll(privilegeHashSet);
        return initDto;
    }
}
